package models

import (
	"log"
	"quantization_trading/src/util"
)

type Strategy struct {
	Id int `json:"id"`
	StrategyName string `json:"strategy_name"` //策略
	AccountName string `json:"account_name"`	//用户名
	Exchange string `json:"exchange"` //交易所
	ContractType string `json:"contract_type"` //合约类型
	Symbol string `json:"symbol"`  //交易对
	Status bool `json:"status"`	//策略是否在使用
	StopLoss float64 `json:"stop_loss"` //止损
	StopProfit float64 `json:"stop_profit"`//止盈
	
	// WildFluctuation相关
	OpenWave	float64  `json:"open_wave"`//开仓波动率
	CloseWave	float64 `json:"close_wave"`//平仓波动率
	Amount	float64    `json:"amount"`//首次购买数量
	Interval	int64  `json:"interval"`//自定义k线间隔(秒)
}

func AddStrategy(strategy *Strategy) int {
	var strategy_list []Strategy
	db.Where("account_name = ?and strategy_name = ?",strategy.AccountName,strategy.StrategyName).Find(&strategy_list)
	if len(strategy_list) == 0{
		if err := db.Create(strategy).Error; err != nil {
			log.Fatal(err)
			return util.ADD_ERROR
		}
		return util.ADD_SUSSESS
	}else {
		return util.ADD_HAVE_DATA
	}
}

func DelStrategy(strategyName, accountName string) int {
	db.Where("account_name = ? and strategy_name = ?",accountName,strategyName).Delete(Strategy{})
	return util.DEL_SUSSESS_STRATEGY
}

func UpdateStrategy(strategyName,accountName string,status bool)  {
	db.Model(Strategy{}).Where("strategy_name = ? and account_name = ?",strategyName,accountName).Updates(map[string]interface{}{"status":status})
}

func FindAllStrategy() []Strategy {
	var strategy_list []Strategy
	db.Find(&strategy_list)
	return strategy_list
}

func FindByAccountNameStrategy(accountName string) []Strategy {
	var strategy_list []Strategy
	db.Where("account_name = ?",accountName).Find(&strategy_list)
	return strategy_list
}